﻿using log4net;
using Sunny.UI;
using System.Configuration;

namespace FZRD

{
    public partial class Page5 : UIPage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Page5));
        private List<SensorEntity> entityList = new List<SensorEntity>();

        public Page5()
        {
            InitializeComponent();

            this.cbWaveChannelSellect.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["cbWaveChannelSellect"]);
            this.cbWaveSensorSellect.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["cbWaveSensorSellect"]);
            this.cbWaveSensorType.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["cbWaveSensorType"]);

            uiDataGridView1.AddColumn("传感器ID", "ID").SetFixedMode(100);
            uiDataGridView1.AddColumn("传感器名", "SensorName").SetFixedMode(150);
            uiDataGridView1.AddColumn("传感器类型", "SensorType").SetFixedMode(150);
            uiDataGridView1.AddColumn("通道号", "ChannelIndex").SetFixedMode(100);
            uiDataGridView1.AddColumn("传感器号", "SensorIndex").SetFixedMode(150);
            uiDataGridView1.AddColumn("波长数量", "WaveCount").SetFixedMode(150);
            uiDataGridView1.AddColumn("表达式", "Expression").SetFixedMode(300);
            uiDataGridView1.AddColumn("描述", "Description").SetFixedMode(300);
            uiDataGridView1.AddColumn("参数1", "Param1").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数2", "Param2").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数3", "Param3").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数4", "Param4").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数5", "Param5").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数6", "Param6").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数7", "Param7").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数8", "Param8").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数9", "Param9").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数10", "Param10").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数11", "Param11").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数12", "Param12").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数13", "Param13").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数14", "Param14").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数15", "Param15").SetFixedMode(100);
            uiDataGridView1.AddColumn("参数16", "Param16").SetFixedMode(100);
        }
        public override void Init()
        {
            base.Init();

            RefreshTable();

        }
        public void RefreshTable()
        {
            entityList.Clear();

            try
            {
                var sql = @"select * from sensor_table";
                var dt = MainForm.db.SelectDataTable(sql);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var row = dt.Rows[i];
                    var func = new SensorEntity()
                    {
                        ID = (int)row["ID"],
                        SensorName = row["SensorName"] != DBNull.Value ? row["SensorName"].ToString() : "",
                        SensorType = row["SensorType"] != DBNull.Value ? (int)row["SensorType"] : 0,
                        ChannelIndex = row["ChannelIndex"] != DBNull.Value ? (int)row["ChannelIndex"] : 0,
                        SensorIndex = row["SensorIndex"] != DBNull.Value ? (int)row["SensorIndex"] : 0,
                        WaveCount = row["WaveCount"] != DBNull.Value ? (int)row["WaveCount"] : 0,
                        Expression = row["Expression"] != DBNull.Value ? row["Expression"].ToString() : "",
                        Description = row["Description"] != DBNull.Value ? row["Description"].ToString() : "",
                        Param1 = row["Param1"] != DBNull.Value ? row["Param1"].ToString() : "",
                        Param2 = row["Param2"] != DBNull.Value ? row["Param2"].ToString() : "",
                        Param3 = row["Param3"] != DBNull.Value ? row["Param3"].ToString() : "",
                        Param4 = row["Param4"] != DBNull.Value ? row["Param4"].ToString() : "",
                        Param5 = row["Param5"] != DBNull.Value ? row["Param5"].ToString() : "",
                        Param6 = row["Param6"] != DBNull.Value ? row["Param6"].ToString() : "",
                        Param7 = row["Param7"] != DBNull.Value ? row["Param7"].ToString() : "",
                        Param8 = row["Param8"] != DBNull.Value ? row["Param8"].ToString() : "",
                        Param9 = row["Param9"] != DBNull.Value ? row["Param9"].ToString() : "",
                        Param10 = row["Param10"] != DBNull.Value ? row["Param10"].ToString() : "",
                        Param11 = row["Param11"] != DBNull.Value ? row["Param11"].ToString() : "",
                        Param12 = row["Param12"] != DBNull.Value ? row["Param12"].ToString() : "",
                        Param13 = row["Param13"] != DBNull.Value ? row["Param13"].ToString() : "",
                        Param14 = row["Param14"] != DBNull.Value ? row["Param14"].ToString() : "",
                        Param15 = row["Param15"] != DBNull.Value ? row["Param15"].ToString() : "",
                        Param16 = row["Param16"] != DBNull.Value ? row["Param16"].ToString() : ""
                    };
                    entityList.Add(func);
                }

                if (entityList.Count > 0)
                {
                    uiPagination1.DataSource = entityList;
                    uiPagination1.ActivePage = 1;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }
        
        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            uiDataGridView1.DataSource = pagingSource;
        }

        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            FAddForm frm = new FAddForm();
            frm.ShowDialog();
            if (frm.IsOK)
            {
                try
                {
                    MainForm.db.ExecuteSql(
                        MainForm.db.Insert("sensor_table")
                            .SetColumn("SensorName", frm.AddRaster.SensorName)
                            .SetColumn("SensorType", frm.AddRaster.SensorType)
                            .SetColumn("ChannelIndex", frm.AddRaster.ChannelIndex)
                            .SetColumn("SensorIndex", frm.AddRaster.SensorIndex)
                            .SetColumn("WaveCount", frm.AddRaster.WaveCount)
                            .SetColumn("Expression", frm.AddRaster.Expression)
                            .SetColumn("Description", frm.AddRaster.Description)
                            .SetColumn("Param1", frm.AddRaster.Param1)
                            .SetColumn("Param2", frm.AddRaster.Param2)
                            .SetColumn("Param3", frm.AddRaster.Param3)
                            .SetColumn("Param4", frm.AddRaster.Param4)
                            .SetColumn("Param5", frm.AddRaster.Param5)
                            .SetColumn("Param6", frm.AddRaster.Param6)
                            .SetColumn("Param7", frm.AddRaster.Param7)
                            .SetColumn("Param8", frm.AddRaster.Param8)
                            .SetColumn("Param9", frm.AddRaster.Param9)
                            .SetColumn("Param10", frm.AddRaster.Param10)
                            .SetColumn("Param11", frm.AddRaster.Param11)
                            .SetColumn("Param12", frm.AddRaster.Param12)
                            .SetColumn("Param13", frm.AddRaster.Param13)
                            .SetColumn("Param14", frm.AddRaster.Param14)
                            .SetColumn("Param15", frm.AddRaster.Param15)
                            .SetColumn("Param16", frm.AddRaster.Param16)
                            .ToSql());
                    RefreshTable();
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                RecordUserOperation("添加传感器 frm.AddRaster=" + frm.AddRaster.ToString());
            }
            frm.Dispose();

        }

        private void btnEdit_Click(object sender, System.EventArgs e)
        {
            int index = this.uiDataGridView1.SelectedIndex;
            SensorEntity sensorEntity = entityList.Get(index);
            if (sensorEntity != null)
            {
                EditRaster editRaster = new EditRaster();
                editRaster.ID = sensorEntity.ID;
                editRaster.SensorName = sensorEntity.SensorName;
                editRaster.SensorType = sensorEntity.SensorType;
                editRaster.ChannelIndex = sensorEntity.ChannelIndex;
                editRaster.SensorIndex = sensorEntity.SensorIndex;
                editRaster.WaveCount = sensorEntity.WaveCount;
                editRaster.Expression = sensorEntity.Expression;
                editRaster.Description = sensorEntity.Description;
                editRaster.Param1 = sensorEntity.Param1;
                editRaster.Param2 = sensorEntity.Param2;
                editRaster.Param3 = sensorEntity.Param3;
                editRaster.Param4 = sensorEntity.Param4;
                editRaster.Param5 = sensorEntity.Param5;
                editRaster.Param6 = sensorEntity.Param6;
                editRaster.Param7 = sensorEntity.Param7;
                editRaster.Param8 = sensorEntity.Param8;
                editRaster.Param9 = sensorEntity.Param9;
                editRaster.Param10 = sensorEntity.Param10;
                editRaster.Param11 = sensorEntity.Param11;
                editRaster.Param12 = sensorEntity.Param12;
                editRaster.Param13 = sensorEntity.Param13;
                editRaster.Param14 = sensorEntity.Param14;
                editRaster.Param15 = sensorEntity.Param15;
                editRaster.Param16 = sensorEntity.Param16;

                FEditForm frm = new FEditForm();
                frm.EditRaster = editRaster;
                frm.ShowDialog();
                if (frm.IsOK)
                {
                    try
                    {
                        MainForm.db.ExecuteSql(
                            MainForm.db.Update("sensor_table")
                                .SetColumn("SensorName", frm.EditRaster.SensorName)
                                .SetColumn("SensorType", frm.EditRaster.SensorType)
                                .SetColumn("ChannelIndex", frm.EditRaster.ChannelIndex)
                                .SetColumn("SensorIndex", frm.EditRaster.SensorIndex)
                                .SetColumn("WaveCount", frm.EditRaster.WaveCount)
                                .SetColumn("Expression", frm.EditRaster.Expression)
                                .SetColumn("Description", frm.EditRaster.Description)
                                .SetColumn("Param1", frm.EditRaster.Param1)
                                .SetColumn("Param2", frm.EditRaster.Param2)
                                .SetColumn("Param3", frm.EditRaster.Param3)
                                .SetColumn("Param4", frm.EditRaster.Param4)
                                .SetColumn("Param5", frm.EditRaster.Param5)
                                .SetColumn("Param6", frm.EditRaster.Param6)
                                .SetColumn("Param7", frm.EditRaster.Param7)
                                .SetColumn("Param8", frm.EditRaster.Param8)
                                .SetColumn("Param9", frm.EditRaster.Param9)
                                .SetColumn("Param10", frm.EditRaster.Param10)
                                .SetColumn("Param11", frm.EditRaster.Param11)
                                .SetColumn("Param12", frm.EditRaster.Param12)
                                .SetColumn("Param13", frm.EditRaster.Param13)
                                .SetColumn("Param14", frm.EditRaster.Param14)
                                .SetColumn("Param15", frm.EditRaster.Param15)
                                .SetColumn("Param16", frm.EditRaster.Param16)
                                .Where("ID=" + frm.EditRaster.ID)
                                .ToSql());

                        RefreshTable();
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                    RecordUserOperation("编辑传感器 frm.EditRaster=" + frm.EditRaster.ToString());
                }
                frm.Dispose();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int index = this.uiDataGridView1.SelectedIndex;
            SensorEntity sensorEntity = entityList.Get(index);
            if (sensorEntity != null)
            {
                try
                {
                    MainForm.db.ExecuteSql(
                        MainForm.db.Delete("sensor_table")
                            .Where("ID=" + sensorEntity.ID)
                            .ToSql());
                    RefreshTable();
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                RecordUserOperation("删除传感器 sensorEntity=" + sensorEntity.ToString());
            }
        }

        private void cbWaveChannelSellect_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.cbWaveChannelSellect.SelectedIndex;
            List<SensorEntity> filterEntityList = new List<SensorEntity>();
            if (entityList.Count > 0)
            {
                for (int i = 0; i < entityList.Count; i++)
                {
                    SensorEntity entity = entityList[i];
                    if (index == entity.ChannelIndex)
                    {
                        filterEntityList.Add(entity);
                    }
                }
                uiPagination1.DataSource = entityList;
            }
        }

        private void cbWaveSensorSellect_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.cbWaveSensorSellect.SelectedIndex;
            List<SensorEntity> filterEntityList = new List<SensorEntity>();
            if (entityList.Count > 0)
            {
                for (int i = 0; i < entityList.Count; i++)
                {
                    SensorEntity entity = entityList[i];
                    if (index == entity.SensorIndex)
                    {
                        filterEntityList.Add(entity);
                    }
                }
                uiPagination1.DataSource = entityList;
            }
        }

        private void cbWaveSensorType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.cbWaveSensorType.SelectedIndex;
            List<SensorEntity> filterEntityList = new List<SensorEntity>();
            if (entityList.Count > 0)
            {
                for (int i = 0; i < entityList.Count; i++)
                {
                    SensorEntity entity = entityList[i];
                    if (index == entity.SensorType)
                    {
                        filterEntityList.Add(entity);
                    }
                }
                uiPagination1.DataSource = entityList;
            }
        }

        public void RecordUserOperation(string operation)
        {
            try
            {
                string userName = LoginForm.username;
                string userIp = "127.0.0.1";
                long recordTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds(); ;
                MainForm.db.ExecuteSql(
                    MainForm.db.Insert("system_operation_content")
                        .SetColumn("UserName", userName)
                        .SetColumn("UserIP", userIp)
                        .SetColumn("RecordTime", recordTime)
                        .SetColumn("Operation", operation)
                        .ToSql());
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }
    }

}